package com.tmsps.ne4Weixin.api;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import com.tmsps.ne4Weixin.api.entity.js.JSConfigModel;
import com.tmsps.ne4Weixin.config.WxConfig;
import com.tmsps.ne4Weixin.encrypt.SHA1;
import com.tmsps.ne4Weixin.utils.PKUtil;
import com.tmsps.ne4Weixin.utils.PaymentUtil;

/**
 * 
 * @author zhangwei 396033084@qq.com MenuAPI 菜单API
 */
public class JsAPI extends BaseAPI {

	public JsAPI(WxConfig config) {
		super(config);
	}

	public JSConfigModel getJsConfigModel(String url) {

		JSConfigModel jsConfig = new JSConfigModel();
		jsConfig.setAppId(config.getAppid());
		jsConfig.setTimestamp(System.currentTimeMillis());
		/**
		 * 随机字符串，不长于32位。
		 */
		jsConfig.setNonceStr(PKUtil.getPK());

		String jsAccessToken = config.getJsAccessToken();

		Map<String, String> params = new HashMap<String, String>();
		params.put("noncestr", jsConfig.getNonceStr());
		params.put("jsapi_ticket", jsAccessToken);
		params.put("timestamp", jsConfig.getTimestamp() + "");
		params.put("url", url);
		try {
			String packageSign = PaymentUtil.packageSign(params, false);
			String sign = SHA1.getSHA1HexString(packageSign);
			jsConfig.setSignature(sign);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return jsConfig;

	}
}
